package com.itany.controller;

import com.itany.entity.User;
import com.itany.vo.ResponseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;

import java.util.HashMap;
import java.util.Map;

// @RestController
// @RequestMapping("/users")
public class UserController {

    @Autowired
    private RestTemplate restTemplate;

    private static final String GLOBAL_URL = "http://localhost:8001/users";

    @GetMapping
    public ResponseResult getUserList(){
        // 发送get请求，参数：请求路径、响应数据类型
        return restTemplate.getForObject(GLOBAL_URL, ResponseResult.class);
    }

    @GetMapping("/{id}")
    public ResponseResult getUser(@PathVariable Integer id){
        // 发送get请求，参数：请求路径、响应数据类型、占位符的值
        return restTemplate.getForObject(GLOBAL_URL+"/{id}", ResponseResult.class, id);
    }

    @PostMapping
    public ResponseResult postUser(User user){
        // 必须使用MultiValueMap来封装参数
        MultiValueMap params = new LinkedMultiValueMap();
        params.add("username",user.getUsername());
        params.add("password",user.getPassword());

        // 发送post请求，参数：请求路径、请求参数、响应数据类型
        return restTemplate.postForObject(GLOBAL_URL, params, ResponseResult.class);
    }

    @DeleteMapping("/{id}")
    public ResponseResult deleteUser(@PathVariable Integer id){
        /**
         * 对于delete和put请求，默认是无法接收返回值
         * 如果有返回值，则需要使用exchange()方法
         */
        // restTemplate.delete();
        // restTemplate.put();

        ResponseEntity<ResponseResult> responseEntity = restTemplate.exchange(GLOBAL_URL + "/{id}", HttpMethod.DELETE, null, ResponseResult.class, id);
        return responseEntity.getBody();
    }

    @PutMapping
    public ResponseResult putUser(User user){
        // 此处只能的url中通过键值对进行参数的传递
        String url = new StringBuffer(GLOBAL_URL)
                .append("?id={id}")
                .append("&username={username}")
                .append("&password={password}")
                .toString();

        Map<String,Object> params = new HashMap<>();
        params.put("id",user.getId());
        params.put("username",user.getUsername());
        params.put("password",user.getPassword());

        ResponseEntity<ResponseResult> responseEntity = restTemplate.exchange(url, HttpMethod.PUT, null, ResponseResult.class, params);
        return responseEntity.getBody();
    }

}
